package user

import (
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"time"
	"weekly-exam-3/config"
)

type Send struct {
	Mobile string `form:"mobile" json:"mobile" xml:"mobile"  binding:"required"`
}

func SendSms(c *gin.Context) {
	var json Send
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	code := rand.Intn(9000) + 1000
	//resp, err := tools.SendSms(json.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return
	//}
	//if *resp.Body.Code != "OK" {
	//	c.JSON(http.StatusBadRequest, gin.H{
	//		"message": *resp.Body.Message,
	//	})
	//	return
	//}
	config.Rdb.Set("sendSms"+json.Mobile, code, time.Minute*5)
	c.JSON(http.StatusOK, gin.H{
		"message": "短信发送成功",
		"code":    200,
	})
}
